package com.robot.member.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.robot.member.pojo.vo.map.Location;
import com.robot.member.pojo.vo.map.baidu.BaiduAddressToPointData;
import com.robot.member.pojo.vo.map.baidu.BaiduAddressToPointResult;
import com.robot.member.pojo.vo.map.tencent.*;
import entity.Point;
import entity.Result;
import entity.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import util.GaoDeMapUtil;
import util.HttpUtil;

import java.util.List;
import java.util.Map;

/**
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/map")
public class MapController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final String successCode = "1";

    /**
     * 腾讯密钥
     */
    String key = "S4RBZ-DWH34-5T5U5-DMWK4-EWRL7-6PBXN";
    /**
     * 百度密钥
     */
    String ak = "fHbXyzoEBhE71PDFGrEMPlVwLeP4Qaya";

    @RequestMapping(value = "/getAddress/{longitude}/{latitude}", method = RequestMethod.GET)
    public Result getPoint(@PathVariable String longitude, @PathVariable String latitude) {
        Map map = (Map) JSON.parse(GaoDeMapUtil.getAddress(longitude, latitude));
        String status = (String) map.get("status");

        if (successCode.equals(status)) {
            return new Result(StatusCode.SUCCESS, map.get("regeocode"));
        }
        return new Result(StatusCode.FAIL);
    }

    @RequestMapping(value = "/getPoint", method = RequestMethod.POST)
    public Result getAddress(@RequestBody Map<String, String> req) {
        String address = req.get("address");
        String city = req.get("city");

        Map map = (Map) JSON.parse(GaoDeMapUtil.getPoint(address, city));
        String status = (String) map.get("status");

        logger.info(map.toString());

        if (successCode.equals(status)) {
            return new Result(StatusCode.SUCCESS, map.get("geocodes"));
        }
        return new Result(StatusCode.FAIL);
    }

    /**
     * 腾讯地址解析/地址转坐标
     * {
     * "address":"厦门市尚忠社290号"
     * }
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "/getPointByTencent", method = RequestMethod.POST)
    public Result getPointByTencent(@RequestBody Map<String, String> req) {
        String address = req.get("address");
        if (address == null) {
            return new Result(StatusCode.ERROR_ADDRESS_REQUIRED);
        }
        //去空格
        address = address.trim();
        String url = "https://apis.map.qq.com/ws/geocoder/v1/?address=" + address + "&key=" + key;
        String response = HttpUtil.httpRequest(url);
        //结果处理方式1-通过层层map获得需要的值
        logger.info(response);
        Map<String, Object> map = (Map<String, Object>) JSON.parse(response);
        logger.info(map.toString());
        JSONObject result = (JSONObject) map.get("result");
        logger.info(result.toString());
        JSONObject location = (JSONObject) result.get("location");
        logger.info(location.toString());

        //结果处理方式2-通过和实体类映射获得需要的值
        TencentAddressToPointData tencentAddressToPointData = JSON.parseObject(response, TencentAddressToPointData.class);
        logger.info("1执行了" + tencentAddressToPointData);
        TencentAddressToPointResult tencentAddressToPointResult = tencentAddressToPointData.getResult();
        logger.info("2执行了" + tencentAddressToPointResult);
        Location tencentAddressToPointLocation = tencentAddressToPointResult.getLocation();
        logger.info("3执行了" + tencentAddressToPointLocation);
        Point point = new Point(String.valueOf(tencentAddressToPointLocation.getLat()), String.valueOf(tencentAddressToPointLocation.getLng()));
        logger.info("4执行了" + point);

        return new Result(StatusCode.SUCCESS, point);
    }

    /**
     * 腾讯逆地址解析
     * {
     * "latitude": "24.52063",
     * "longitude": "118.149231",
     * "getPoi":1
     * }
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "/getAddressByTencent", method = RequestMethod.POST)
    public Result getAddressByTencent(@RequestBody Map<String, String> req) {
        String latitude = req.get("latitude");
        if (latitude == null) {
            return new Result(StatusCode.ERROR_LATITUDU_REQUIRED);
        }
        String longitude = req.get("longitude");
        if (longitude == null) {
            return new Result(StatusCode.ERROR_LONGITUDE_REQUIRED);
        }
        //去空格
        latitude = latitude.trim();
        longitude = longitude.trim();
        //getPoi-是否返回周边POI列表：1.返回；0不返回(默认)
        int getPoi = 0;
        if ("1".equals(req.get("getPoi"))) {
            getPoi = 1;
        }
        logger.info("getPoi=" + getPoi);


        String url = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + latitude + "," + longitude + "&key=" + key + "&get_poi=" + getPoi;
        String response = HttpUtil.httpRequest(url);
        //结果处理方式1-通过层层map获得需要的值


        //结果处理方式2-通过和实体类映射获得需要的值
        TencentPointToAddressData tencentPointToAddressData = JSON.parseObject(response, TencentPointToAddressData.class);
        logger.info("1执行了" + tencentPointToAddressData);
        TencentPointToAddressResult tencentPointToAddressResult = tencentPointToAddressData.getResult();
        logger.info("2执行了" + tencentPointToAddressResult);
        String address = tencentPointToAddressResult.getAddress();
        logger.info("3执行了" + address);
        TencentAddressToPointAddressComponents address_component = tencentPointToAddressResult.getAddress_component();
        logger.info("4执行了" + address_component);
        int poiCount = tencentPointToAddressResult.getPoiCount();
        List<TencentPointToAddressPoi> pois = tencentPointToAddressResult.getPois();
        logger.info("4执行了" + poiCount + "," + pois);

        return new Result(StatusCode.SUCCESS, address);
    }

    /**
     * 百度地址解析/地址转坐标
     * {
     * "address":"厦门市尚忠社290号"
     * }
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "/getPointByBaidu", method = RequestMethod.POST)
    public Result getPointByBaidu(@RequestBody Map<String, String> req) {
        String address = req.get("address");
        if (address == null) {
            return new Result(StatusCode.ERROR_ADDRESS_REQUIRED);
        }
        //去空格
        address = address.trim();

        String url = "http://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + ak;
        String response = HttpUtil.httpRequest(url);
        //结果处理方式1-通过层层map获得需要的值

        //结果处理方式2-通过和实体类映射获得需要的值
        BaiduAddressToPointData baiduAddressToPointData = JSON.parseObject(response, BaiduAddressToPointData.class);
        logger.info("1执行了" + baiduAddressToPointData);
        BaiduAddressToPointResult baiduAddressToPointResult = baiduAddressToPointData.getResult();
        logger.info("2执行了" + baiduAddressToPointResult);
        Location location = baiduAddressToPointResult.getLocation();
        Point point = new Point(String.valueOf(location.getLat()), String.valueOf(location.getLng()));

        return new Result(StatusCode.SUCCESS, point);
    }
}


